package base.utils

import android.util.Log
import com.blankj.utilcode.util.LogUtils

object LogUtil {

    private var logGate = true

    private val TAG = "LogUtil"
    /**
     * 打开
     *
     * @param b
     */
    fun openGate(b: Boolean) {
        logGate = b
    }

    fun i(text: String) {
        if (logGate) {
            Log.i(TAG + "info", buildMessage(text))
        }
    }

    fun d(text: String) {
        if (logGate) {
            Log.d(TAG + "debug", buildMessage(text))
        }
    }

    fun e(text: String) {
        if (logGate) {
            Log.e(TAG + "error", buildMessage(text))
            LogUtils.file("时间戳-"+System.currentTimeMillis()+":"+text)
        }
    }

    fun v(text: String) {
        if (logGate) {
            Log.v(TAG + "verbose", buildMessage(text))
        }
    }

    fun w(string: String) {
        if (logGate) {

            Log.w(TAG + "warn", buildMessage(string))
        }
    }

    fun i(tag: String, text: String) {
        if (logGate) {
            Log.i(TAG + tag, buildMessage(text))
        }
    }

    fun d(tag: String, text: String) {
        if (logGate) {
            Log.d(TAG + tag, buildMessage(text))
        }
    }

    fun e(tag: String, text: String) {
        if (logGate) {
            Log.e(TAG + tag, buildMessage(text))
            LogUtils.file("时间戳-"+System.currentTimeMillis()+":"+tag+"="+text)
        }
    }

    fun v(tag: String, text: String) {
        if (logGate) {
            Log.v(TAG + tag, buildMessage(text))
        }
    }

    fun w(tag: String, text: String) {
        if (logGate) {
            Log.w(TAG + tag, buildMessage(text))
        }
    }

    /**
     * Building Message
     *
     * @param msg The message you would like logged.
     * @return Message String
     */
    private fun buildMessage(msg: String): String {
        val caller = Throwable().fillInStackTrace().stackTrace[2]

        return StringBuilder().append(" [").append(caller.fileName).append(".")
                .append(caller.methodName).append("#").append(caller.lineNumber.toString() + "]")
                .append(msg).toString()
    }
}